<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 11:02
 */

namespace app\common\model;


use think\console\command\make\Model;
use think\Db;

class Banner extends Model{

    //banner列表
    public function index($page, $where)
    {
        $list = Db::name('banner')
            ->where($where)
            ->field(['id', 'title', 'photo', 'status', 'create_time', 'update_time'])
            ->order('id desc')
            ->paginate(15, false, ['page' => $page]);
        if (sizeof($list) == 0) {
            return false;
        }
        return $list;
    }

    //添加Bannner
    public function adddata($params)
    {
        return Db::name('banner')->insertGetId([
            'title'        => $params['title'],
            'photo'        => $params['photo'][0],
            'status'       => $params['status'],
            'create_time'   => time(),
            'update_time'   => time()
        ]);


    }

    //根据ID获取Banner详情
    public function detail($id)
    {
        return Db::name('banner')
            ->where('id', $id)
            ->field(['id', 'title', 'photo'])
            ->find();
    }

    //修改Banner
    public function update($params)
    {
        return Db::name('banner')
            ->where('id', intval($params['id']))
            ->update([
                'title'         => $params['title'],
                'photo'         => $params['photo'][0],
                'update_time'    => time()
            ]);
    }

    //启用 禁用
    public function edit($id, $status)
    {
        return Db::name('banner')->where('id', $id)->update(['status' => $status, 'update_time' => time()]);
    }


    //前台首页获取Banner
    public function getBanner()
    {
        return Db::name('banner')->where('status', 1)->order('id', 'desc')->field(['id', 'title', 'photo'])->select();
    }
}